package dao;

// Generated Mar 18, 2014 10:39:39 PM by Hibernate Tools 3.4.0.CR1
import java.util.Iterator;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import pojo.Mobile;
import pojo.User;

/**
 * Home object for domain model class User.
 *
 * @see pojo.User
 * @author Hibernate Tools
 */
public class UserHome {

    private final Session session;

    public UserHome(SessionFactory sessionFactory) {
        session = sessionFactory.openSession();
    }

    public void createUser(User user) {
        session.beginTransaction();
        session.persist(user);
        session.getTransaction().commit();
    }

    public void removeUser(User user) {
        session.beginTransaction();
        session.delete(user);
        session.getTransaction().commit();
    }

    public User getUserById(int id) {
        return (User) session.load(User.class, id);
    }

    public List getAllDescUsers() {
        return session.createCriteria(User.class)
                .addOrder(Order.desc("id"))
                .list();
    }

    public void updateUser(User user) {
        session.beginTransaction();
        session.update(user);
        session.getTransaction().commit();
    }

    public List findByExample(User user) {
        return session.createCriteria("pojo.User")
                .add(Example.create(user))
                .list();
    }
    
    public Integer verifyUser(String email, String pass) {
        return (Integer) session.createCriteria(User.class).setProjection(Projections.property("id"))
                .add(Restrictions.eq("email", email))
                .add(Restrictions.eq("password", pass))
                .add(Restrictions.eq("activated", true)).uniqueResult();
    }

    public List getProducts(int userId) {
        return session.createCriteria(Mobile.class).setProjection(Projections.projectionList()
                .add(Projections.property("id")).add(Projections.property("name")).add(Projections.property("price")))
                .createCriteria("userHasMobiles").add(Restrictions.eq("checkedOut", false))
                .createCriteria("user").add(Restrictions.eq("id", userId)).list();
    }
    
    public void avtivateUser(User user) {
        
    }

}
